package com.sise.service.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.sise.service.SensitiveWordService;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author idea
 * @data 2019/6/8
 */
public class SensitiveWordServiceImpl implements SensitiveWordService {

    private Directory directory;

    private IndexReader indexReader;

    private IndexSearcher indexSearcher;


    /**
     * 敏感词过滤 基于ik分词器 优点：匹配够灵敏 缺点：一些新型的关键字需要手动补充维护，较为麻烦
     *
     * @param wordContent 敏感词汇
     * @return
     */
    @Override
    public  List<String> sensitveWordFilterByIk(String wordContent) {
        //使用ik分词器分出来的效果会比较好一些
        Analyzer iKAnalyzer = new IKAnalyzer();
        try {
            return buildAnalyzerDoc(iKAnalyzer,wordContent);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }


    /**
     * 全文匹配 基于step步长设置 优点：所有可能出现的词语都会匹配到 缺点：一些非必要文字也要进行匹配
     *
     * @param wordContent
     * @param step
     * @return
     */
    @Override
    public List<String> sensitiveWordFilterByStep(String wordContent, int step) {
        if(StringUtils.isBlank(wordContent)){
            return Collections.emptyList();
        }
        int index=0;
        int len=wordContent.length();
        List<String> wordList=new ArrayList<>();
        while(index+step<len){
            String item=wordContent.substring(index,index+step);
            wordList.add(item);
            index++;
        }
        return wordList;
    }


    public static void main(String[] args) throws IOException {
        SensitiveWordServiceImpl sensitiveWordService=new SensitiveWordServiceImpl();
        sensitiveWordService.sensitveWordFilterByIk("色情,服务");
    }


    /**
     * 构建分词信息
     *
     * @param analyzer 过滤的分词器类型，中文语言统一使用ik分词器
     * @param text 待分词文本
     * @throws IOException
     */
    private List<String> buildAnalyzerDoc(Analyzer analyzer, String text) throws IOException {
        TokenStream tokenStream = analyzer.tokenStream("filterWordObj", new StringReader(text));
        CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
        tokenStream.reset();
        List<String> resultList=new ArrayList<>();
        while (tokenStream.incrementToken()) {
            resultList.add(charTermAttribute.toString());
        }
        tokenStream.end();
        tokenStream.close();
        analyzer.close();
        return resultList;
    }


}
